<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line('payment_confirm');?></h3>
<?php

if($paymentAtHome->ticketWorkers == ""){
    echo 'Vé chưa giao! Vui lòng phải giao vé trước rồi mới xác nhận!';
}
?>
<style>
    .name_num_cls{
        width:20%;
    }
    .fone_num_cls{
        width:10%;
    }
</style>
<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" paymentAtHome-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
    <?php echo form_open();?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php 
                //mã KH
                echo form_label($this->lang->line('customerID'), 'customerID');
                echo form_input(array('name' => 'customerID', 'id' => 'customerID', 'value' => $paymentAtHome->customerID,'readonly' => 'true'));
                echo form_error('customerID');
                //số dư của KH
                $sodu = $this->paymentAtHome_model->getInfoContactsByParams(array('key'=>'customerID','value'=>$paymentAtHome->customerID));
                echo form_label($this->lang->line('surplus'), 'surplus');
                echo form_input(array('name' => 'surplus_fake', 'id' => 'surplus_fake', 'value' => ($sodu->surplus != NULL)?$sodu->surplus:'0','readonly' => 'true'));
                echo form_input(array('type'=>'hidden', 'name' => 'surplus', 'id' => 'surplus', 'value' => ($sodu->surplus != NULL)?$sodu->surplus:'0'));
                echo form_error('surplus');
                //số tiền thực nhận
                echo form_label($this->lang->line('thucnhan'), 'thucnhan_fake');
                echo form_input(array('name' => 'thucnhan_fake', 'id' => 'thucnhan_fake', 'value' => '','required'=>'required'));
                echo form_error('thucnhan_fake');
                echo form_input(array('type' => 'hidden', 'name' => 'thucnhan', 'id' => 'thucnhan', 'value' => ''));
                //số tiền thực lưu
                echo form_label($this->lang->line('thucluu'), 'thucluu_fake');
                echo form_input(array('name' => 'thucluu_fake', 'id' => 'thucluu_fake', 'value' => '','required'=>'required'));
                echo form_error('thucluu_fake');
                echo form_input(array('type' => 'hidden', 'name' => 'thucluu', 'id' => 'thucluu', 'value' => ''));
                //khoản dư
                echo form_label($this->lang->line('residual'), 'residual');
                echo form_input(array('name' => 'residual_fake', 'id' => 'residual_fake', 'value' => '0'));
                echo form_input(array('type'=>'hidden', 'name' => 'residual', 'id' => 'residual', 'value' => '0'));
                echo form_error('residual');
                //Ghi chú
                echo form_label($this->lang->line('note'), 'note');
                echo form_textarea(array('name' => 'note', 'id' => 'note', 'value' => $paymentAtHome->note,'required'=>'required','rows'=>'5'));
                echo form_error('note');
                
                //hidden field
                $advance = $this->paymentAtHome_model->getInfoBookingHold($paymentAtHome->booking_id);//getInfoBookingHold
                echo form_input(array('type'=>'hidden', 'name' => 'payment', 'id' => 'payment', 'value' => '[AtHome]'));
                echo form_input(array('type'=>'hidden', 'name' => 'id', 'value' => $paymentAtHome->id));
                echo form_input(array('type'=>'hidden', 'name' => 'booking_id', 'value' => $paymentAtHome->booking_id));
                echo form_input(array('type'=>'hidden', 'name' => 'advance', 'value' => $advance[0]->advance));
                echo form_input(array('type'=>'hidden', 'name' => 'booker', 'value' => $paymentAtHome->owner));
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')); ?>
                <a href="<?php echo site_url('auth/paymentAtHome'); ?>" class="btn"><?php echo $this->lang->line('cancel')?></a>
        </div>
    <?php form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>
<?php $this->load->view('templates/backend/lib'); ?>

<script src="<?php echo STATIC_URL.'backend/js/autoNumeric.js';?>"></script>
<script>
$(document).ready(function() {
    var open = '<?php echo $paymentAtHome->ticketWorkers;?>';
    if(open===""){
        $('button[type="submit"]').attr('disabled','disabled');
        $('#amount_fake').attr('disabled','disabled');
        $('#note').attr('readonly','disabled');
        alert("Chưa có nhân viên giao vé! Vui lòng phân quyền giao vé!");
        setTimeout("location.href = '/auth/paymentAtHome';",1000);
        return false;
    }
    $( "#payment_method" ).change(function() {
        $('#note').removeAttr('readonly');
        $('#submit').removeAttr('disabled');
        updateURL = '/auth/booking/paymentMethod?id='+$(this).val();
        $.ajax(
        {
                url : updateURL,
                success:function(data)
                {
                   $("#payment_method_form").html(data);
                },
                error: function() 
                {
                    $("#simple-msg").html('Vui lòng liên hệ Admin!');
                }
        });
    });
    
    $('form').submit(function() {
            $("#simple-msg-submit").html("Vui lòng chờ... <img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/> ");
            setTimeout(function () {
                $("#simple-msg-submit").hide();
            },2000);
            $(this).find("button[type='submit']").prop('disabled',true);
    });
    
});

$(function() {
  var $datepicker = $( "#maturity" );
  $datepicker.datepicker({ dateFormat: 'dd/mm/yy' }).val();
});

//số dư
$('#surplus_fake').autoNumeric('init',{aSign:' VND',mDec:0, pSign:'s' });
$("#surplus_fake").blur(function() {
    document.getElementById("surplus").value = UnFormatNumber($('#surplus_fake').val());
});
//thực nhận
$('#thucnhan_fake').autoNumeric('init',{aSign:' VND',mDec:0, pSign:'s' });
$("#thucnhan_fake").blur(function() {
    document.getElementById("thucnhan").value = UnFormatNumber($('#thucnhan_fake').val());
    document.getElementById("thucluu").value = Number($('#thucnhan').val()) + Number($('#surplus').val());
    document.getElementById("thucluu_fake").value = FormatNumber(document.getElementById("thucluu").value);
});
//thực lưu
$('#thucluu_fake').autoNumeric('init',{aSign:' VND',mDec:0, pSign:'s' });
$("#thucluu_fake").on('blur',function() {
    document.getElementById("thucluu").value = UnFormatNumber($('#thucluu_fake').val());
});
//khoản dư của khách
$('#residual_fake').autoNumeric('init',{aSign:' VND',mDec:0, pSign:'s' });
$("#residual_fake").blur(function() {
    document.getElementById("residual").value = UnFormatNumber($('#residual_fake').val());
});
    
function UnFormatNumber(x) {
    if (typeof x === "undefined") {
        return '';
    } else {
    return x.toString().replace(/,|VND|\s/g, "");
    }
};

function FormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
        }
};
</script>
